package cn.buk.tms.delivery.service;

import cn.buk.common.JsonResult;
import cn.buk.tms.dao.DeliveryNoteDao;
import cn.buk.tms.dao.FlightOrderDao;
import cn.buk.tms.dao.OrderNoGenerator;
import cn.buk.tms.entity.misc.DeliveryNote;
import cn.buk.tms.entity.order.flight.FlightOrder;
import cn.buk.tms.entity.misc.OperationLog;
import cn.buk.tms.entity.order.OrderNoInfo;
import cn.buk.tms.sc.SearchFlightOrderCriteria;
import cn.buk.tms.delivery.dto.DeliveryNoteDto;
import cn.buk.tms.flight.util.FlightOrderUtil;
import cn.buk.tms.util.ValidationUtils;
import cn.buk.common.util.DateUtil;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author yfdai
 */
@Component
public class DeliveryNoteServiceImpl implements DeliveryNoteService {

  @Autowired
  private DeliveryNoteDao dao;

  @Autowired
  private FlightOrderDao flightOrderDao;

  @Autowired
  private OrderNoGenerator orderNoGenerator;

  @Override
  public List<DeliveryNote> searchDeliveryNotes(final int enterpriseId,
      SearchFlightOrderCriteria sc) {

    List<DeliveryNote> list = dao.searchDeliveryNotes(enterpriseId, sc);
    for(DeliveryNote deliveryNote: list) {
      if (deliveryNote.getDeliveryOrderId() == 0) {
        if (OrderNoInfo.ORDER_FLIGHT.equalsIgnoreCase(deliveryNote.getDeliveredOrderType())) {
          //获取
          FlightOrder flightOrder = flightOrderDao.getOrderDetailByOrderNo(deliveryNote.getEnterpriseId(), deliveryNote.getDeliveredOrderNo());
          if (flightOrder != null) {
            //保存机票订单的ID，方便网页上的跳转链接
            deliveryNote.setDeliveryOrderId(flightOrder.getId());
          }
        }
      }
    }

    return list;
  }

  @Override
  public DeliveryNoteDto getDeliveryNoteById(final int enterpriseId, final int orderId) {
    DeliveryNote o = dao.getDeliveryNoteById(orderId);
    if (o == null || o.getEnterpriseId() != enterpriseId) {
      return null;
    }

    DeliveryNoteDto dto = DeliveryNoteDto.createDto(o);

    if (OrderNoInfo.ORDER_FLIGHT.equalsIgnoreCase(dto.getDeliveredOrderType())) {
      //机票订单
      FlightOrder flightOrder = flightOrderDao.getOrderDetailByOrderNo(enterpriseId, dto.getDeliveredOrderNo());
      FlightOrderUtil.clean(flightOrder);
      dto.setFlightOrder(flightOrder);
    }

    return dto;
  }

  @Override
  public JsonResult processDeliveryNote(final int enterpriseId, final String username, final int orderId) {
    DeliveryNote o = dao.getDeliveryNoteById(orderId);
    if (o == null || o.getEnterpriseId() != enterpriseId) {
      return JsonResult.createJsonResult(-1, "无此配送单");
    }

    if (o.getDeliveryDate() == null) {
      return JsonResult.createJsonResult(-2, "配送日期不能为空");
    }

    if (o.getDeliveryDate().getTime() < DateUtil.getCurDate().getTime()) {
      return JsonResult.createJsonResult(-3, "配送日期不能早于今天");
    }

    int retCode = 0;
    try {
      retCode = dao.processDeliveryNote(enterpriseId, username, orderId);
    } catch (RuntimeException ex) {
      retCode = -100;
    }

    return JsonResult.createJsonResult(retCode);
  }

  @Override
  public JsonResult finishDeliveryNote(int enterpriseId, String username, int orderId,
      String remark) {
    int retCode = 0;
    try {
      retCode = dao.finishDeliveryNote(enterpriseId, username, orderId, remark);
    } catch (RuntimeException ex) {
      retCode = -100;
    }

    return JsonResult.createJsonResult(retCode);
  }

  @Override
  public JsonResult cancelDeliveryNote(int enterpriseId, String username, int orderId,
      String remark) {
    int retCode = 0;
    try {
      retCode = dao.cancelDeliveryNote(enterpriseId, username, orderId, remark);
    } catch (RuntimeException ex) {
      retCode = -100;
    }

    return JsonResult.createJsonResult(retCode);
  }

  @Override
  public JsonResult updateDeliveryNoteDeliveryDate(int enterpriseId, String username, int orderId,
      Date deliveryDate) {
    int retCode = 0;
    try {
      retCode = dao.updateDeliveryNoteDeliveryDate(enterpriseId, username, orderId, deliveryDate);
    } catch (RuntimeException ex) {
      retCode = -100;
    }

    return JsonResult.createJsonResult(retCode);
  }

  @Override
  public List<OperationLog> searchDeliveryNoteHistory(int enterpriseId, int orderId) {
    return dao.searchDeliveryNoteHistory(enterpriseId, orderId);
  }

  @Override
  public JsonResult createDeliveryNote(int enterpriseId, String username, DeliveryNote dn) {
    try {
      ValidationUtils.validate(dn);
    } catch (Exception e) {
      e.printStackTrace();
      return JsonResult.createJsonResult(-1, e.getMessage());
    }

    dn.setOperator(username);
    dn.setOrderNo(orderNoGenerator.generateOrderNo(enterpriseId, OrderNoInfo.DELIVERY_NOTE));



    int retCode = 0;
    String errmsg = null;
    try {
      retCode = dao.createDeliveryNote(dn);
    } catch (RuntimeException ex) {
      ex.printStackTrace();
      errmsg = ex.getMessage();
      retCode = -100;
    }

    return JsonResult.createJsonResult(retCode, errmsg);

  }
}
